<script setup lang="ts">
  import { ref } from 'vue'
  import type { LoginEmp } from '@/api/model/model'
  import router from "@/router";
  import { loginApi } from "@/api/login";
  import { ElMessage } from "element-plus";
  import { useLogingEmpStore } from "@/stores/LoginEmp";//导入pinia定义存储文件中存储对象
  //导入路由实例对象

  let loginForm = ref<LoginEmp>({username:"", password:""});
  const token = ref<String>();

  //获取pinia存储对象
  const loginEmpStore = useLogingEmpStore();

  //登录的点击事件函数
  const login = async () => {
    //调用远程登录接口
    const result = await loginApi(loginForm.value);
    //判断调用是否成功
    if (result.code) {
      //成功，提示"登录的成功"
      ElMessage.success("记忆力不错嘛，Ciallo～(∠・ω< )⌒☆");

      //将登录数据存储到pinia中
      loginEmpStore.setLoginEmp(result.data);

      //跳转到首页"/index"
      router.push("/index");
    } else {
      //失败，显示失败消息
      ElMessage.error("错了，像沐沐一样的笨逼")
    }
  };

  //取消点击事件
  const clear = () => {
    //重置表单数据
    loginForm.value = { username: "", password: ""};
  };
</script>

<template>
  <div id="container">
    <div class="login-form">
      <el-form label-width="80px">
        <p class="title">Tlias智能学习辅助系统</p>
        <el-form-item label="用户名" prop="username">
          <el-input v-model="loginForm.username" placeholder="请输入用户名"></el-input>
        </el-form-item>

        <el-form-item label="密码" prop="password">
          <el-input type="password" v-model="loginForm.password" placeholder="请输入密码"></el-input>
        </el-form-item>

        <el-form-item>
          <el-button class="button" type="primary" @click="login">登 录</el-button>
          <el-button class="button" type="info" @click="clear">重 置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<style scoped>
#container {
  padding: 10%;
  height: 410px;
  background-image: url('../../assets/bg1.jpg');
  background-repeat: no-repeat;
  background-size: cover;
}

.login-form {
  max-width: 400px;
  padding: 30px;
  margin: 0 auto;
  border: 1px solid #e0e0e0;
  border-radius: 10px;
  box-shadow: 0 0 10px rgba(0, 0, 0, 0.5);
  background-color: white;
}

.title {
  font-size: 30px;
  font-family: '楷体';
  text-align: center;
  margin-bottom: 30px;
  font-weight: bold;
}

.button {
  margin-top: 30px;
  width: 120px;
}
</style>